package com.liu.audio.tts;

import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesizer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class TTS2File {
  private static String apikey = "sk-d1c41de66c074fc686488ab227d32fbe";
  private static String model = "cosyvoice-v1";
  private static String voice = "longxiaochun";

  public static void main(String[] args) {
    process();
    System.exit(0);
  }


  public static void process() {
    SpeechSynthesisParam param = SpeechSynthesisParam.builder()
            .apiKey(apikey)
            .model(model)
            .voice(voice)
            .build();
    SpeechSynthesizer synthesizer = new SpeechSynthesizer(param, null);
    ByteBuffer audio = synthesizer.call("今天天气怎么样？");

    System.out.print("requestId: " + synthesizer.getLastRequestId());

    // 输出文件
    File file = new File("output.mp3");
    try (FileOutputStream fos = new FileOutputStream(file)) {
      fos.write(audio.array());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }


}